<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\DeviceService;
use Illuminate\Http\Request;

class DeviceController extends BaseController
{
    //设备商城

    public function __construct(DeviceService $caseService)
    {
        $this->db = $caseService;
    }

    //参数列表
    public function getPasIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $type = $request->get('type', 0);
        $res = $this->db->getPasIndex($type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //参数更新
    public function pasModify(Request $request)
    {
        $id = $request->post('id', 0);
        $type = $request->post('type', 0);
        $pas['title'] = $request->post('title', '');
        if ($id) {
            $res = $this->db->pasModify($id, $pas);
        } else {
            $pas['type'] = $type;
            if (!$pas['type']) {
                return $this->toJson(-2, '分类必须的');
            }
            $res = $this->db->pasAdd($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //参数更新
    public function pasDel(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->pasDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //选项
    public function getOption(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $pages['start'] = $request->get('page', 1);
        $pages['limit'] = $request->get('limit', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
//
//    //详情
//    public function getOne(Request $request)
//    {
//        //header("Access-Control-Allow-Origin: *");
//        $id = $request->get('id', 0);
//        $res = $this->db->getOne($id);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson(0, '', $res['data']);
//    }

//    //添加
//    public function add(Request $request)
//    {
//        header("Access-Control-Allow-Origin: *");
//        $pas = $request->all();
//        $type = isset($pas['type']) ? $pas['type'] : ''; //业务类型
//        if (!$type) {
//            return $this->toJson(-2, '业务类型必须的');
//        }
//        unset($pas['token']); unset($pas['s']);
//        unset($pas['id']); unset($pas['type']);
//        $res = (new YewuService())->add($type, $pas);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }
}